import {LOGIN, ROUTES,BASEURL} from '@/services/api'
import {request, METHOD, removeAuthorization} from '@/utils/request'

/**
 * 登录服务
 * @param name 账户名
 * @param password 账户密码
 * @returns {Promise<AxiosResponse<T>>}
 */
export async function login(username, password,captcha) {
  return request(LOGIN, METHOD.POST, {
    username: username,
    password: password,
    captcha: captcha
  })
}

export async function getRoutesConfig() {
  return request(ROUTES, METHOD.GET)
}
/*
* 用户列表
* */
export async function getUsers(nickName,sex,start,end,pageNo,pageSize){
  return request(BASEURL+'/user/users',METHOD.POST,{
    nickName:nickName,
    sex:sex,
    start:start,
    end:end,
    pageNo:pageNo,
    pageSize:pageSize
  })
}
/*
* 根据编号删除用户
* */
export async function deleteById(id){
  return request(BASEURL+'/user/delete/'+id,METHOD.DELETE)
}
/*
* 添加用户
* */
export async function addUser(username,nickName,password,phone,email){
  return request(BASEURL+'/user/addUser',METHOD.POST,{
    username:username,
    nickName:nickName,
    password:password,
    phone:phone,
    email:email
  })
}
/*
* 修改用户状态
* */
export async function updateStatus(id){
  return request(BASEURL+'/user/update/'+id,METHOD.GET)
}
/*
* 获取用户信息
* */
export async function getById(id){
  return request(BASEURL+'/user/getById/'+id,METHOD.GET)
}
/*
* 修改用户信息
* */
export async function updateUserInfo(id,nickName,sex,password,phone,email){
  return request(BASEURL+'/user/updateUser',METHOD.POST,{
    id:id,
    nickName:nickName,
    sex:sex,
    password:password,
    phone:phone,
    email:email
  })
}
//用户退出
export async function userLogout(){
  return request(BASEURL+'/user/logout',METHOD.GET)
}
/**
 * 退出登录
 */
export function logout() {
  localStorage.removeItem(process.env.VUE_APP_ROUTES_KEY)
  localStorage.removeItem(process.env.VUE_APP_PERMISSIONS_KEY)
  localStorage.removeItem(process.env.VUE_APP_ROLES_KEY)
  localStorage.removeItem('user')
  localStorage.removeItem('roles')
  removeAuthorization()
}
export default {
  login,
  logout,
  getRoutesConfig
}
